	//
	//  JsBottomInputBar.m
	//  JsBottomInputBar
	//
	//  Created by Gikki Ares on 2020/12/21.
	//  Copyright © 2020 vgemv. All rights reserved.
	//

#import "JsBottomInputBar.h"
#import <JsUtilitySdk/JsUtilitySdk.h>
#import "JsExpressionChooseView.h"
#import "JsVoiceInputView.h"
#import "AGEmojiKeyBoardView.h"
@interface JsBottomInputBar()<
JsButtonLiiOneDelegate,
AGEmojiKeyboardViewDelegate,
AGEmojiKeyboardViewDataSource,
JsTextViewDelegate,
UITextViewDelegate,
JsVoiceInputViewDelegate
>



{
	JsTextViewVm * mvm_textView;
	JsButtonLiiOne * mbtn_voice;
	JsButtonLiiOne * mbtn_expression;
		//	GaoButtonLii * mbtn_keyboard;
	JsButtonLiiOne * mbtn_send;
	AGEmojiKeyboardView * mExpressionChooseView;
	JsVoiceInputView * mVoiceInputView;
	BOOL mb_isTextEmpty;
	NSString * mstr_placeholder;
	
	JsTextView * mtv;
	
	UIView * originSuperView;
	
	CGPoint mPt_origin;
}

@property(nonatomic,strong) JsButtonLiiOne * mbtn_keyboard;

@end

const static int ButtonTag_Keyboard = 3;
const static int ButtonTag_Expression = 4;
const static int ButtonTag_Voice = 5;
const static int ButtonTag_Send = 6;

@implementation JsBottomInputBar

//loadNib加载
- (void)awakeFromNib {
	[super awakeFromNib];
	mPt_origin = self.frame.origin;
	[self commonInit];
}

	//view初始化,创建关联的vm. //vm还是不默认创建了.
//initWithFrame->setFrame
- (instancetype)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame]) {
		mPt_origin = frame.origin;
			//首先把vm创建好,vm是呈现的数据
		[self commonInit];
	}
	return self;
}
//
- (void)setComputedFrame {
	CGSize size  = _vm.computedSize;
	self.frame = CGRectMake(mPt_origin.x, mPt_origin.y, size.width, size.height);
}

- (void)commonInit {
	mb_isTextEmpty = YES;
	self.vm = [JsBottomInputBarVm sharedInstance];
	
		//监听键盘弹出的代理
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardShow:) name:UIKeyboardWillShowNotification object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
	
}



- (void)setVm:(JsBottomInputBarVm *)vm {
	_vm = vm;
	[self setupUi];
}


- (JsButtonLiiOne *)mbtn_keyboard {
		//Keyboard按钮;
	if(!_mbtn_keyboard) {
		_mbtn_keyboard = [[JsButtonLiiOne alloc] init];
		_mbtn_keyboard.hidden = YES;
		_mbtn_keyboard.tag = ButtonTag_Keyboard;
		_mbtn_keyboard.delegate = self;
		[self addSubview:_mbtn_keyboard];
		UIImage * image = [UIImage imageNamed:@"JsBottomInputBar_Keyboard.png"];
		[_mbtn_keyboard setImage:image];
	}
	return _mbtn_keyboard;
}


	//根据当前的状态来进行显示,所有的显示都最终在这里进行设置.
	//这样就避免了导出寻找
	//如果有显示的问题,就一定能在这里找到方法解决.
	//state0 + action = state1;
- (void)setupUi {
	[self initExpressionBtn];
	[self initVoiceBtn];
	[self setupTextView];
	if(self.vm.isExpaneded) {
//		self.backgroundColor = self.vm.expanedBackgroundColor;
	}
	else {
//		self.backgroundColor = self.vm.normalBackgroundColor;
	}
	
	if(self.vm.isExpaneded&&!mVoiceInputView) {
			//要延伸时,同时创建input和expression
		mVoiceInputView = [[JsVoiceInputView alloc]initWithFrame:self.vm.voiceInputViewFrame];
		mVoiceInputView.delegate = self;
		[self addSubview:mVoiceInputView];
		
		originSuperView = self.superview;
		[self.window addSubview:self];
		
		
		AGEmojiKeyboardView * expressionChooseView = [[AGEmojiKeyboardView alloc]initWithFrame:self.vm.expressionViewFrame dataSource:self];
		mExpressionChooseView = expressionChooseView;
			//MARK:为什么设置的是100,但是确是200?
			//此选项会重新布局大小,不需要.
		expressionChooseView.autoresizingMask = UIViewAutoresizingNone;
		expressionChooseView.delegate = self;
		
			//inputView的作用,是系统自动弹出这个view.这里我们不设置,自己弹出.
			//			mtv.textView.inputView = expressionChooseView;
			//			mtv.textView.inputView = nil;
		
		[mtv resignFirstResponder];
		[self addSubview:expressionChooseView];
		
		[CATransaction begin];
		[CATransaction setDisableActions:YES];
		float y = [originSuperView convertPoint:self.frame.origin toView:self.window].y;
		CGRect startFrame = [JsGeometryManager rectWithX:0 y:y size:self.frame.size];
		startFrame = [self adjustFameToSafeArea:startFrame];
		[self.window addSubview:self];
			//		CGRect frame = [GaoGeometryManager frameCenterHorozontallyWithSize:self.vm.computedSize bottomSpacing:0 containerSize:self.window.frame.size];
		self.frame = startFrame;
		[CATransaction commit];
		
		
		
			//显示到window上去.
		JsDisplayStyle * style = [JsDisplayStyle defaulStyle];
		style.displayAnimationType = JsDisplayAnimationTypeNone;
		style.undisplayAnimationType = JsUndisplayAnimationTypeNone;
			//		style.mf_maskAlpha = 1;
		style.maskColor = [UIColor clearColor];
		style.displayPosition = JsDisplayPositionBottom;
		if(self.window) {
			NSLog(@"Add botom bar to window.");
			[self displayInView:self.window withDisplayStyle:style undisplayCompletionHandler:^{
				NSLog(@"Bottom bar not expand.");
					//MARK: 收起键盘,更新状态,更新Ui
				[self finishEditing];
			}];
		}
		else {
			NSLog(@"Window is nil");
		}
		
	}
	
	
	
	
		//需要调用子组件的setupUi.
	[mtv setupUi];
	
	{
			//sendBtn
		if(!mbtn_send) {
			mbtn_send = [[JsButtonLiiOne alloc] initWithFrame:[self.vm sendBtnFrame]];
			mbtn_send.tag = ButtonTag_Send;
			mbtn_send.delegate = self;
			[self addSubview:mbtn_send];
			JsButtonLiiVm * uip = [mbtn_send vm];
			uip.foregroundImageViewFrame  = [JsGeometryManager frameAlignCenterWithContainerSize:mbtn_expression.frame.size size:CGSizeMake(JsBottomInputBarVm.btnImageSize, JsBottomInputBarVm.btnImageSize)];
			[mbtn_send setupUi];
		}
		mbtn_send.frame = self.vm.sendBtnFrame;
		if(self.vm.isEmpty) {
			UIImage * image = [UIImage imageNamed:@"JsBottomInputBar_Send_Gray.png"];
			[mbtn_send setImage:image];
		}
		else {
			UIImage * image = [UIImage imageNamed:@"JsBottomInputBar_Send_Red.png"];
			[mbtn_send setImage:image];
		}
		[mbtn_send setupUi];
	}
	
	
	
	{
		
			//设置ExpressionChooseView
		if(self.vm.isExpaneded) {
			if(self.vm.isExpressionDisplay) {
					//打开表情界面.
					//初次创建
				mExpressionChooseView.frame = self.vm.expressionViewFrame;
					//				float y =  self.window.frame.size.height - self.vm.computedSize.height;
					//				CGRect frame = CGRectMake(0, y, self.vm.computedSize.width, self.vm.computedSize.height);
					//				frame = [self adjustFameToSafeArea:frame];
					//				frame = [self adjustFameToSafeArea:frame];
					//				frame = [self adjustFameToSafeArea:frame];
					//				self.frame = frame;
				
			}
			else {
					//MARK: 表情view不显示,但是如果创建了还是要根据vm来更新ui数据.
					//此时整体的frame在keyborad中设置
				mExpressionChooseView.frame = self.vm.expressionViewFrame;
			}
		}
	}
	
	{
		
		if(self.vm.isExpressionDisplay) {
			mbtn_expression.hidden = YES;
		}
		else {
			mbtn_expression.hidden = NO;
		}
		
	}
	
		//键盘,声音,表情按钮的逻辑
		//键盘input,声音 input的逻辑
	{
		if(self.vm.isExpaneded) {
			if(self.vm.isExpressionDisplay) {
				self.mbtn_keyboard.hidden = NO;
				self.mbtn_keyboard.frame = self.vm.expressionBtnFrame;
				JsButtonLiiVm * uip = [self.mbtn_keyboard vm];
				uip.foregroundImageViewFrame  = [JsGeometryManager frameAlignCenterWithContainerSize:self.mbtn_keyboard.frame.size size:CGSizeMake(JsBottomInputBarVm.btnImageSize, JsBottomInputBarVm.btnImageSize)];
				[self.mbtn_keyboard setupUi];
				
				mbtn_expression.hidden = YES;
				mbtn_voice.hidden = NO;
				
				mVoiceInputView.hidden = YES;
				mExpressionChooseView.hidden = NO;
				
			}
			else if(self.vm.isVoiceDisplay) {
				self.mbtn_keyboard.hidden = NO;
				self.mbtn_keyboard.frame = self.vm.voiceBtnFrame;
				JsButtonLiiVm * uip = [self.mbtn_keyboard vm];
				uip.foregroundImageViewFrame = [JsGeometryManager frameAlignCenterWithContainerSize:self.mbtn_keyboard.frame.size size:CGSizeMake(JsBottomInputBarVm.btnImageSize, JsBottomInputBarVm.btnImageSize)];
				[self.mbtn_keyboard setupUi];
				mbtn_voice.hidden = YES;
				
				mVoiceInputView.hidden = NO;
				mExpressionChooseView.hidden = YES;
			}
			else if(self.vm.isKeyboardDisplay) {
				self.mbtn_keyboard.hidden = YES;
				mbtn_voice.hidden = NO;
				mbtn_expression.hidden = NO;
				
				mVoiceInputView.hidden = YES;
				mExpressionChooseView.hidden = YES;
			}
		}
		else {
			self.mbtn_keyboard.hidden = YES;
			mbtn_voice.hidden = NO;
			mbtn_expression.hidden = NO;
			
			mVoiceInputView.hidden = YES;
			mExpressionChooseView.hidden = YES;
		}
		mbtn_expression.frame = self.vm.expressionBtnFrame;
		mbtn_voice.frame = self.vm.voiceBtnFrame;
		mVoiceInputView.frame = self.vm.voiceInputViewFrame;
		mExpressionChooseView.frame = self.vm.expressionViewFrame;
		
	}
	
		//设置自己的frame,每个时机的frame都可能改变.只在这里设置.
	if(self.vm.isExpaneded) {
		if(self.vm.isKeyboardDisplay) {
			[UIView animateWithDuration:0.2 animations:^{
				float y = self.window.frame.size.height - self.vm.keyboardHegiht - self.vm.headerHeight;
				CGRect newFrame = self.frame;
				newFrame.origin.y = y;
				newFrame.size = self.vm.computedSize;
					//keyborad不需要增加,因为键盘已经加了
				self.frame = newFrame;
			}];
		}
		else if(self.vm.isVoiceDisplay||self.vm.isExpressionDisplay) {
			[UIView animateWithDuration:0.2 animations:^{
					//				CGRect rect = [GaoGeometryManager frameCenterHorozontallyWithSize:self.vm.computedSize bottomSpacing:0 containerSize:self.window.frame.size];
				CGRect rect = [JsGeometryManager frameAlignCenterWithContainerSize:self.window.frame.size size:self.vm.computedSize];
				rect = [self adjustFameToSafeArea:rect];
				self.frame = rect;
			}];
		}
			//如果是键盘弹起来,在通知里更新,因为需要根据键盘高度更新.
		
	}
	else {
			//缩起来,
		if(mExpressionChooseView) {
				//MARK: 收起键盘的Frame改变.
			[mExpressionChooseView removeFromSuperview];
			mExpressionChooseView = nil;
			
			[mVoiceInputView removeFromSuperview];
			mVoiceInputView = nil;
			
				//初始状态.计算window中当前位置在origin中的坐标.因为父视图发生了改变. 如果frame不变,会导致真实位置不对.
			CGFloat y = [self.window convertPoint:self.frame.origin toView:originSuperView].y;
			
			[self undisplay];
			[self->originSuperView addSubview:self];
			[self->originSuperView bringSubviewToFront:self];
			self.hidden = NO;
			CGRect startFrame = [JsGeometryManager rectWithX:0 y:y size:self.frame.size];
			startFrame = [self adjustFameToSafeArea:startFrame];
			
			CGRect endFrame = [JsGeometryManager frameHAlignCenterVAlignBottomWithContainerSize:originSuperView.frame.size size:self.vm.computedSize];
			endFrame = [self adjustFameToSafeArea:endFrame];
			
			CGRect midFrame = [JsGeometryManager rectWithX:0 y:endFrame.origin.y size:self.frame.size];
			midFrame = [self adjustFameToSafeArea:midFrame];
			
			self.frame = startFrame;
			[UIView animateWithDuration:0.2 animations:^{
				self.frame = midFrame;
			} completion:^(BOOL finished) {
				self.frame = endFrame;
			}];
		}
	}
}

- (CGRect)adjustFameToSafeArea:(CGRect)frame {
	if (@available(iOS 11.0, *)) {
		float safeAresBottom = self.superview.safeAreaInsets.bottom;
		frame.origin.y -= safeAresBottom;
	}
	return frame;
	
}

//status 应该配合 action来进行处理.
- (void)finishEditing {
	if(self.vm.isKeyboardDisplay) {
		[mtv.textView resignFirstResponder];
	}
	else {
		self.vm.isExpaneded = NO;
		self.vm.isExpressionDisplay = NO;
		self.vm.isVoiceDisplay = NO;
		[mVoiceInputView stopVoiceInput];
		self.vm.isKeyboardDisplay = NO;
		[self.vm doCalculate];
		if(self.delegate && [self.delegate respondsToSelector:@selector(jsBottomBar:finishEditingWithMessage:)]) {
			[self.delegate jsBottomBar:self finishEditingWithMessage:self.vm.content];
		}
	}
}

- (void)setupTextView {
	if(!mtv) {
			//new 调用的是initWithFrame:CGRectZero
		mtv = [[JsTextView alloc]initWithFrame:self.vm.textViewFrame];
		mtv.textView.delegate = self;
		[self addSubview:mtv];
		mtv.delegate = self;
		mtv.vm = self.vm.textViewVm;
	}
	
	self.vm.textViewVm.content = self.vm.content;
	mtv.frame = self.vm.textViewFrame;
	[mtv setupUi];
}



- (void)initExpressionBtn {
	if(!mbtn_expression) {
		mbtn_expression = [[JsButtonLiiOne alloc] initWithFrame:[self.vm expressionBtnFrame]];
		mbtn_expression.tag = ButtonTag_Expression;
		mbtn_expression.delegate = self;
		[self addSubview:mbtn_expression];
		JsButtonLiiVm * uip = [mbtn_expression vm];
		uip.foregroundImageViewFrame = [JsGeometryManager frameAlignCenterWithContainerSize:mbtn_expression.frame.size size:CGSizeMake(JsBottomInputBarVm.btnImageSize, JsBottomInputBarVm.btnImageSize)];
		UIImage * image = [UIImage imageNamed:@"JsBottomInputBar_Expression.png"];
		[mbtn_expression setImage:image];
		[mbtn_expression setupUi];
	}
	mbtn_expression.frame = self.vm.expressionBtnFrame;
	
}

- (void)initVoiceBtn {
	if(!mbtn_voice) {
		mbtn_voice = [[JsButtonLiiOne alloc] initWithFrame:[self.vm voiceBtnFrame]];
		mbtn_voice.delegate = self;
		[self addSubview:mbtn_voice];
		mbtn_voice.tag = ButtonTag_Voice;
		JsButtonLiiVm * uip = [mbtn_voice vm];
		uip.mTextAlignment = NSTextAlignmentLeft;
		uip.labelFont = [UIFont systemFontOfSize:15];
		[mbtn_voice setTitle:mstr_placeholder];
			//	[uip absolutePositionImageViewVerticallyWithLeading:15 height:22 width:20];
			//
		uip.foregroundImageViewFrame = [JsGeometryManager frameAlignCenterWithContainerSize:mbtn_expression.frame.size size:CGSizeMake(JsBottomInputBarVm.btnImageSize, JsBottomInputBarVm.btnImageSize)];
		
		UIImage * image = [UIImage imageNamed:@"JsBottomInputBar_Microphone.png"];
		[mbtn_voice setImage:image];
		
		[mbtn_voice setupUi];
	}
	mbtn_voice.frame = self.vm.voiceBtnFrame;
}

#pragma mark 2 InnerFunction

#pragma mark 3 Interface
- (JsBottomInputBarVm *)getUiParameter {
	return self.vm;
}

- (void)clickVoiceButton {
		//	[mbtn_voice toggle];
	[self jsButtonLiiOneClicked:mbtn_voice];
}

- (void)setPlaceholder:(NSString *)placeholder {
	mstr_placeholder = placeholder;
	NSString * text = self.vm.content;
	if(text.length) {
		NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"[草稿]%@",text] attributes:@{NSFontAttributeName:[mbtn_voice textFont]}];
		[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
		[mbtn_voice updateAttributedText:str];
		
	}
	else {
		[mbtn_voice setTitle:mstr_placeholder];
	}
}


- (void)setDraftText:(NSString *)text {
	self.vm.content = text;
	if(text.length) {
		NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"[草稿]%@",text] attributes:@{NSFontAttributeName:[mbtn_voice textFont]}];
		[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
		[mbtn_voice updateAttributedText:str];
		
	}
	else {
		[mbtn_voice setTitle:mstr_placeholder];
	}
}

- (NSString *)draftText {
	if(self.vm.content.length) {
		return self.vm.content;
	}
	else {
		return nil;
	}
}


#pragma mark 4 Delegate
- (void)jsButtonLiiOneClicked:(JsButtonLiiOne *)button {
	switch (button.tag) {
		case ButtonTag_Keyboard: {
			NSLog(@"Click keyboard");
			self.vm.isExpaneded = YES;
			self.vm.isExpressionDisplay = NO;
			self.vm.isVoiceDisplay = NO;
			[mVoiceInputView stopVoiceInput];
			self.vm.isKeyboardDisplay = YES;
			[mtv.textView becomeFirstResponder];
			[self.vm doCalculate];
			[self setupUi];
			break;
		}
		case ButtonTag_Expression: {
			NSLog(@"Click expression");
				//			GaoExpressionChooseView * expressionChooseView = [[GaoExpressionChooseView alloc]initWithFrame:CGRectMake(0, 30, GaoGeometryManager.screenWidth, 300)];
			
				//MARK: 点击表情按钮,更新状态,更新ui
			self.vm.isExpressionDisplay = YES;
			self.vm.isKeyboardDisplay = NO;
			self.vm.isVoiceDisplay = NO;
			[mVoiceInputView stopVoiceInput];
			self.vm.isExpaneded = YES;
			[self->mtv.textView resignFirstResponder];
			[self.vm doCalculate];
			[self setupUi];
			break;
		}
		case ButtonTag_Voice: {
			NSLog(@"Click voice");
			self.vm.isExpaneded = YES;
			self.vm.isKeyboardDisplay = NO;
			self.vm.isExpressionDisplay = NO;
			self.vm.isVoiceDisplay = YES;
			[self->mtv.textView resignFirstResponder];
			[self.vm doCalculate];
			[self setupUi];
			break;
		}
		case ButtonTag_Send: {
			if(self.vm.content.length) {
				NSLog(@"Click send:%@",self.vm.content);
				if(self.delegate && [self.delegate respondsToSelector:@selector(jsBottomBar:sendMessage:)]) {
					[self.delegate jsBottomBar:self sendMessage:self.vm.content];
				}
				self.vm.content = @"";
				[self undisplay];
				[self finishEditing];
			}
		}
		default:{
				//			if(_delegate&&[_delegate respondsToSelector:@selector(gaoBottomBarClickVoice:)]) {
				//				[_delegate gaoBottomBarClickVoice:self];
				//			}
			break;
		}
	}
}


#pragma mark AGEmojiKeyboardViewDelegate AGEmojiKeyboardViewDataSource
	//选择了一个emoji
- (void)emojiKeyBoardView:(AGEmojiKeyboardView *)emojiKeyBoardView didUseEmoji:(NSString *)emoji {
	NSString * str = @"";
	if(self.vm.content.length) {
		str = self.vm.content;
	}
	str = [str stringByAppendingString:emoji];
		//调用此方法更新ui
	self.vm.content = str;
	[self.vm doCalculate];
		//vm更新,需要updateui.
	[self setupUi];
	
}

	//退格
- (void)emojiKeyBoardViewDidPressBackSpace:(AGEmojiKeyboardView *)emojiKeyBoardView {
	if(self.vm.content.length>0) {
		NSString * str = self.vm.content;
			//遍历字符串中的每个字符
		NSRange fullRange = NSMakeRange(0, [self.vm.content length]);
		__block NSRange lastCharacterRange = NSMakeRange(0, 0);
		[str enumerateSubstringsInRange:fullRange
								options:NSStringEnumerationByComposedCharacterSequences|NSStringEnumerationReverse
							 usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
			lastCharacterRange = substringRange;
			*stop = YES;
				//			NSLog(@"%@ %@ %@", substring, NSStringFromRange(substringRange),NSStringFromRange(enclosingRange));
		}];
		str = [self.vm.content stringByReplacingCharactersInRange:lastCharacterRange withString:@""];
		self.vm.content = str;
		[self.vm doCalculate];
		[self setupUi];
		
	}
}

- (UIColor *)randomColor {
	return [UIColor colorWithRed:drand48()
						   green:drand48()
							blue:drand48()
						   alpha:drand48()];
}

- (UIImage *)randomImage {
	CGSize size = CGSizeMake(30, 10);
	UIGraphicsBeginImageContextWithOptions(size , NO, 0);
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	UIColor *fillColor = [self randomColor];
	CGContextSetFillColorWithColor(context, [fillColor CGColor]);
	CGRect rect = CGRectMake(0, 0, size.width, size.height);
	CGContextFillRect(context, rect);
	
	fillColor = [self randomColor];
	CGContextSetFillColorWithColor(context, [fillColor CGColor]);
	CGFloat xxx = 3;
	rect = CGRectMake(xxx, xxx, size.width - 2 * xxx, size.height - 2 * xxx);
	CGContextFillRect(context, rect);
	
	UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return img;
}

- (UIImage *)emojiKeyboardView:(AGEmojiKeyboardView *)emojiKeyboardView imageForSelectedCategory:(AGEmojiKeyboardViewCategoryImage)category {
	switch (category) {
		case AGEmojiKeyboardViewCategoryImageRecent:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_history.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageFace:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_smile.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageBell:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_food.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageFlower:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_animal.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageCar:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_building.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageCharacters:
		default:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_sign.png"];
			return img;
		}
	}
}

- (UIImage *)emojiKeyboardView:(AGEmojiKeyboardView *)emojiKeyboardView imageForNonSelectedCategory:(AGEmojiKeyboardViewCategoryImage)category {
	switch (category) {
		case AGEmojiKeyboardViewCategoryImageRecent:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_history.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageFace:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_smile.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageBell:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_food.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageFlower:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_animal.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageCar:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_building.png"];
			return img;
		}
		case AGEmojiKeyboardViewCategoryImageCharacters:
		default:{
			UIImage *img = [UIImage imageNamed:@"gaoinputbox_sign.png"];
			return img;
		}
	}
}

- (UIImage *)backSpaceButtonImageForEmojiKeyboardView:(AGEmojiKeyboardView *)emojiKeyboardView {
	UIImage *img = [UIImage imageNamed:@"gaoinputbox_backspace.png"];
	return img;
}

- (AGEmojiKeyboardViewCategoryImage)defaultCategoryForEmojiKeyboardView:(AGEmojiKeyboardView *)emojiKeyboardView {
	return AGEmojiKeyboardViewCategoryImageFace;
}

-(void)gaoTextViewClick:(JsTextView *)tv {
	NSLog(@"Click text view.");
}

#pragma mark UITextFieldDelegate
	//当输入中文时,会首先显示字母,此时也会调用此方法.
	//如果直接更新当前的文字,会造系统输入法不能输入中文?
- (void)textViewDidChange:(UITextView *)textView {
	NSLog(@"textViewDidChange");
	NSString * text = textView.text;
	self.vm.content = text;
	[self.vm doCalculate];
	[self setupUi];
}

	//MARK: 点击键盘的Done,或者textView不成为第一响应者,都会触发此方法.
- (void)textViewDidEndEditing:(UITextView *)textView {
	NSLog(@"TextViewDidEndEditing...");
	if(self.vm.isKeyboardDisplay) {
			//如果是键盘显示的时候,点击done才消失.
		[self finishEditing];
	}
}


#pragma mark 5 Notification
	//MARK: 直接点击输入框
- (void)textViewDidBeginEditing:(UITextView *)textView {
	NSLog(@"TextView begin editing");
		//已经成为了第一响应者.键盘会自动起来.
}

#pragma mark 键盘弹起
- (void)onKeyboardShow:(NSNotification *)notification {
	
		//	NSLog(@"show!!!");
		//	mb_isChangeKeyboard = NO;
		//	mvm.mb_isRised = YES;
		//	if(!mvm.mb_isRising) {
		//		mvm.mb_isRising = YES;
		//		CGFloat animationTime = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
		//键盘的高度
	CGRect keyboardFrame = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
	CGFloat keyboardHeight = keyboardFrame.size.height;
	
		//		NSLog(@"KeyboardHeight is %.2f",keyboardHeight);
		//		mvm.mf_keyboardHeight = keyboardHeight;
	
	
	self.vm.keyboardHegiht = keyboardHeight;
	self.vm.isKeyboardDisplay = YES;
	self.vm.isExpressionDisplay = NO;
	self.vm.isVoiceDisplay = NO;
	[mVoiceInputView stopVoiceInput];
	self.vm.isExpaneded = YES;
	[self.vm doCalculate];
	[self setupUi];
	
		//MARK: 禁用默认的键盘弹起效果,有点奇怪.
		//		[CATransaction begin];
		//		[CATransaction setDisableActions:YES];
		//
		//		[CATransaction commit];
	
	
	
		//		[UIView setAnimationsEnabled:NO];
		//		[UIView setAnimationsEnabled:YES];
	
	
	
		//		mvm.mb_isRising = FALSE;
	
		//	}
}

- (void)onKeyboardHide:(NSNotification *)notification {
	[CATransaction begin];
	[CATransaction setDisableActions:YES];
	[CATransaction commit];
	
	if(self.vm.isExpaneded == NO) {
			//如果Expanded=NO才是结束输入.
		[self finishEditing];
	}
}

- (void)gaoVoiceInputView:(JsVoiceInputView *)view getText:(NSString *)text {
	NSLog(@"Get text from audio:%@",text);
	NSString * str = @"";
	if(self.vm.content) {
		str = self.vm.content;
	}
	self.vm.content = [NSString stringWithFormat:@"%@%@",str,text];
	[self.vm doCalculate];
	[self setupUi];
}

- (void)awakeKeyboard {
	[mtv.textView becomeFirstResponder];
}
- (void)updatePlaceholder:(NSString *)placeholder{
	[mtv setPlaceholder:placeholder];
}

@end
